$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'staff/searchStaffInfo.do',
        datatype: "json",
        colModel: [
            { label: '员工编号', name: 'staffNo', index: "staffNo", width: 100, key: true },
            { label: '员工名称', name: 'staffName', index: "staffName", width: 90 },
            { label: '性别', name: 'gender', sortable: false, width: 90 },
            { label: '身份证号', name: 'idCard', sortable: false, width: 90 },
            { label: '出生日期', name: 'birthDateStr', sortable: false, width: 90 },
            { label: '地址', name: 'address', width: 100 },
            { label: '手机号', name: 'phone', width: 80 },
            { label: '入职日期', name: 'entryDateStr', width: 80 },
            { label: '工作性质', name: 'job', width: 100 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
        	 root: "page.list",
             page: "page.currPage",
             total: "page.totalPage",
             records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        staffName:null,
        showList: true,
        showStaffNo:false,
        title:null,
        staff:{
        	     staffNo:null,
        		 staffName:null,
        		 gender:null,
        		 idCard:null,
        		 birthDate:null,
        		 address:null,
        		 phone:null,
        	     entryDate:null,
        		 job:null
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.title = "新增";
            vm.staff = {
       	     staffNo:null,
       		 staffName:null,
       		 gender:null,
       		 idCard:null,
       		 birthDate:null,
       		 address:null,
       		 phone:null,
       	     entryDate:null,
       		 job:null
             };
            vm.showList = false;
        },
        update: function () {
            var staffNo = getSelectedRow();
            if(staffNo == null){
                return ;
            }
            var entryDateStr="";
            var birthDateStr="";
            vm.title = "修改";
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "staff/getStaffByNo.do?staffNo="+staffNo,
                contentType: "application/json",
                success: function(r){
                    if(r.code === 0){
                    	vm.staff=r.result;
                    	entryDateStr=r.result.entryDateStr;
                    	birthDateStr=r.result.birthDateStr;
                    }
                }
            });
            vm.showStaffNo=true;
            vm.showList = false;
            $("#entryDate").val(entryDateStr);
        	$("#birthDate").val(birthDateStr);
        },
        del: function () {
            var staffNos = getSelectedRows();
            if(staffNos == null){
                return ;
            }
            var flag=confirm('确定要删除选中的记录？');
            if(flag){
                $.ajax({
                    type: "POST",
                    url: baseURL + "/staff/deleteByNo.do",
                    contentType: "application/json",
                    data: JSON.stringify(staffNos),
                    success: function(r){
                        if(r.code == 0){
                        	alert("删除成功");
                        	 vm.reload();
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            }
        },
        saveOrUpdate: function () {
        	var staffName=vm.staff.staffName;
        	var gender=vm.staff.gender;
        	var idCard=vm.staff.idCard;
        	var job=vm.staff.job;
        	var entryDate=$("#entryDate").val();
        	var birthDate=$("#birthDate").val();
        	vm.staff.entryDate=entryDate;
        	vm.staff.birthDate=birthDate;
        	var msg="";
        	if(staffName==""||staffName==null){
        		msg=msg+"员工名称不能为空,"
        	}
        	if(gender==""||gender==null){
        		msg=msg+"性别不能为空,"
        	}
        	if(idCard==""||idCard==null){
        		msg=msg+"身份证号码不能为空,"
        	}
        	if(job==""||job==null){
        		msg=msg+"工作性质不能为空,"
        	}
        	if(msg!=""){
        		 alert(msg);
        	}else{
        		var url = vm.staff.staffNo == null ? "staff/add.do" : "staff/updateStaffInfo.do";
        		$.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.staff),
                    success: function(r){
                        if(r.code === 0){
                        	alert("操作成功");
                        	vm.reload();
                        }else{
                            alert(r.msg);
                        }
                    }
                });
        	}
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'staffName': vm.staffName},
                page:page
            }).trigger("reloadGrid");
        }
    }
});